package org.iteam.xlz.core.service.order;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.iteam.commons.utils.DateUtil;
import org.iteam.core.utils.SqlBuilder;
import org.iteam.db.BaseService;
import org.iteam.xlz.core.entity.order.OrderInfo;
import org.iteam.xlz.core.entity.order.OrderInfoRetry;
import org.iteam.xlz.core.entity.user.UserInfo;

public class RefundService extends BaseService<OrderInfoRetry>{
	
	private static RefundService instance;
	
	private RefundService(){}
	
	public static RefundService getInstance(){
		if(null==instance){
			instance=new RefundService();
		}
		return instance;
	}
	
	public long counts(){
		try {
			return super.counts("select count(1) from orderinforetry where 1=1");
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	public List<OrderInfoRetry> query(long page,long rows){
		try {
			return super.query("select * from orderinforetry where 1=1 order by createtime desc limit ?,?",(page-1)*rows,rows);
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<OrderInfoRetry>();
		}
	}
	
	public boolean create(OrderInfoRetry model){
		Connection con=null;
		try {
			con=super.getConnection(false);
			
			OrderInfo order=super.show(OrderInfo.class,con,"select * from orderinfo where oid=?",model.getOid());
			SqlBuilder sql1=SqlBuilder.createInsert("orderinforetry");
			sql1.addField("oirid",UUID.randomUUID().toString().replace("-","").toUpperCase());
			sql1.addField("amount",model.getAmount());
			sql1.addField("oid",model.getOid());
			sql1.addField("tradeNo",order.getTradeNo());
			sql1.addField("reason",model.getReason());
			sql1.addField("channels",order.getPaytype());
			sql1.addField("createtime",DateUtil.currentTime());
			sql1.addField("status",0);
			sql1.addField("creator",model.getCreator());
			int i=super.updates(con,sql1.getSql(),sql1.getParams());
			SqlBuilder sql2=SqlBuilder.createUpdate("orderinfo");
			sql2.addField("status",3);
			sql2.addField("updatetime",DateUtil.currentTime());
			sql2.addWhere("oid",model.getOid());
			sql2.addWhere("status",1);
			int j=super.updates(con, sql2.getSql(),sql2.getParams());
			if(i==1&&j==1){
				super.commit(con);
				return true;
			}else{
				super.rollback(con);
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				super.rollback(con);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
		return false;
	}
	
	public boolean confirm(OrderInfoRetry model){
		Connection con=null;
		try {
			con=super.getConnection(false);
			if(model.getChannels()==0){
				OrderInfo order=super.show(OrderInfo.class,con,"select * from orderinfo where oid=?",model.getOid());
				SqlBuilder sql1=SqlBuilder.createUpdate("orderinforetry");
				sql1.addField("status",1);
				sql1.addField("operator",model.getOperator());
				sql1.addField("retrytime",DateUtil.currentTime());
				sql1.addWhere("oirid",model.getOirid());
				sql1.addWhere("status",0);
				int i=super.updates(con,sql1.getSql(),sql1.getParams());
				SqlBuilder sql2=SqlBuilder.createInsert("logaccount");
				sql2.addField("laid",UUID.randomUUID().toString().replace("-","").toUpperCase());
				sql2.addField("uid",order.getUid());
				sql2.addField("amount",model.getAmount());
				sql2.addFieldSql("items","订单退款");
				sql2.addField("logtype",1);
				sql2.addField("createtime",DateUtil.currentTime());
				int j=super.updates(con,sql2.getSql(),sql2.getParams());
				UserInfo user=super.show(UserInfo.class,con,"select * from userinfo where uid=?",order.getUid());
				BigDecimal money=new BigDecimal(user.getDaole());
				BigDecimal amount=new BigDecimal(model.getAmount());
				BigDecimal r=money.add(amount);
				SqlBuilder usql=SqlBuilder.createUpdate("userinfo");
				usql.addField("daole",r.toString());
				usql.addWhere("uid",user.getUid());
				usql.addWhere("daole",user.getDaole());
				int k=super.updates(con,usql.getSql(),usql.getParams());
				if(i==1&&j==1&&k==1){
					super.commit(con);
					return true;
				}else{
					super.rollback(con);
					return false;
				}
			}else{
				SqlBuilder sql=SqlBuilder.createUpdate("orderinforetry");
				sql.addField("operator",model.getOperator());
				sql.addField("retrytime",DateUtil.currentTime());
				sql.addField("thirdid",model.getThirdid());
				sql.addField("status",1);
				sql.addWhere("oirid",model.getOirid());
				sql.addWhere("status",0);
				int i=super.updates(con,sql.getSql(),sql.getParams());
				if(i==1){
					super.commit(con);
					return true;
				}else{
					return false;
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				super.rollback(con);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
		return false;
	}
}
